var prefix = jsctx + "/elecStation";

var securityPrevention = (function () {

    //删除优惠项
    $(function () {
        $(".layui-card").on("click", ".delList", function () {
            $(this).parents(".layui-card-body").addClass("hide").attr("delList",true);
            $(this).parents(".layui-card-body").find(".limitBatteryTemp").val("");
            if($(".coupon-layui-card-body-battery.hide").length<5){
                $(".addBatteryList").removeClass("hide");
            }
        })
        $(".layui-card").on("click", ".delSOCList", function () {
            $(this).parents(".layui-card-body").addClass("hide").attr("delList",true);
            $(this).parents(".layui-card-body").find(".limitSOCTemp").val("");
            if($(".coupon-layui-card-body-SOC.hide").length<5){
                $(".addSOCList").removeClass("hide");
            }
        })
    })

    // return {
    //     addBatteryList: addBatteryList,
    //     addSOCList: addSOCList
    // };
})();

var addSecurityPrevention = (function () {

    function init() {
        validateRule();
    };

    function getCheckedRoles() {
        var adIds = "";
        $("input:checkbox[name=role]:checked").each(function (i) {
            if (0 == i) {
                adIds = $(this).val();
            } else {
                adIds += ("," + $(this).val());
            }
        });
        return adIds;
    }

    var isajaxsend = true;

    function save() {
        if (isajaxsend) {
            var batteryList = [];
            var socList = [];
            var required = true;

            $(".coupon-layui-card-body-battery").each(function () {
                if(!$(this).hasClass("hide")){
                    var batteryListObj = {};
                    var limitBatteryTemp = $(this).find(".limitBatteryTemp").val();
                    var stationId = $(this).find(".select-picker-search-checked").attr("stationId");
                    batteryListObj.temp = limitBatteryTemp;
                    batteryListObj.stationId = stationId;
                    batteryList.push(batteryListObj)
                }
            })

            $(".coupon-layui-card-body-SOC").each(function () {
                if(!$(this).hasClass("hide")){
                    var socListObj = {};
                    var limitSOCTemp = $(this).find(".limitSOCTemp").val();
                    var stationId = $(this).find(".select-picker-search-checked").attr("stationId");
                    socListObj.temp = limitSOCTemp;
                    socListObj.stationId = stationId;
                    socList.push(socListObj)
                }
            })



            if (!required) {
                return;
            }

            var formData = new FormData($("#frmRechargeDiscount")[0]);
            formData.append("batteryList", JSON.stringify(batteryList));
            formData.append("socList", JSON.stringify(socList));
            formData.append("openChargeLimitBatteryTemp", $("input[name='openChargeLimitBatteryTemp']:checked").val());
            formData.append("openChargeLimitSoc", $("input[name='openChargeLimitSoc']:checked").val());
           // formData.append("marketActivityId", $("#marketActivityId").val() ? $("#marketActivityId").val() : '');
            $("#roleIds").val(getCheckedRoles());
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/saveOrUpdateSecurityPrevention",
                data: formData,
                processData: false,
                contentType: false,
                async: false,
                beforeSend: function () {
                    isajaxsend = false
                },
                error: function (request) {
                    isajaxsend = true
                    parent.layer.alert("Connection error");
                },
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        window.history.back();
                    } else {
                        parent.layer.alert(data.message)
                    }

                },complete:function () {
                    isajaxsend = true
                }
            });
        }
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmSecurityPrevention").validate({
            rules: {},
            messages: {},
            submitHandler: function (form) {
                save();
            }
        })
    }

    return {
        init: init
    };
})();
